//
//  EQAnalysisCell.swift
//  EQQuestionBank
//
//  Created by Macx on 2018/8/4.
//  Copyright © 2018年 FanWeiJian. All rights reserved.
//

import UIKit
import SnapKit
import WebKit

class EQAnalysisCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.setupSubviews()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupSubviews(){
        webView.snp.makeConstraints { (make) in
            make.bottom.top.equalToSuperview()
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(1)
        }
    
    }
    
    lazy var webView: WKWebView = {
        let web = WKWebView()
        web.navigationDelegate = self
        
        self.addSubview(web)
        return web
    }()

}

extension EQAnalysisCell : WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        webView.evaluateJavaScript("document.body.offsetHeight") {[weak self] (resule, error) in
            if error == nil {
                print(resule!)
                let height = resule as! CGFloat
                var frame = self!.frame
                frame.size.height =  frame.size.height + height
                self!.frame = frame
                self!.webView.snp.updateConstraints { (make) in
                    make.height.equalTo(height)
                }
                
                self!.layoutIfNeeded()
            }
        }
    }
    

    
    
    
}
